<?php


namespace api\controllers;


use api\components\BaseController;
use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\Order;
use common\models\OrderReceiv;
use common\models\Running;
use Yii;

/**
 * 跑腿
 * Class RunningControltroller
 * @package api\controllers
 */
class RunningController extends BaseController
{
    public $model = null;
    public function init()
    {
        parent::init();
        $this->model = new Running();
    }

    /**
     * 入驻
     * @return mixed
     */
    public function actionSettled()
    {
        $post = Yii::$app->request->post();
        return $this->model->settled(self::$userid,$post);
    }

    /**
     * 接单中心
     * @return array
     */
    public function actionReceiv()
    {
        $get = Yii::$app->request->get();
        $page = $get['page']??1;
        $model = new Order();
        $res = (new Running())->isRunn(self::$userid);
        if (!$res['is_runn']){
            return commonApi::message('无法接单',false);
        }
        $andWhere = ['and'];
        $where = ['run_status'=>0,'order_status'=>1,'delivery_method'=>2];
        if ($res['campus_id']){
            $where['campus_id'] = $res['campus_id'];
        }
        $andWhere[] = $where;
        if (!empty($res['info']['sex'])){
            $andWhere[] = ['in','snatch_sex',[0,$res['info']['sex']]];
        }
        return $model->getList('',$andWhere,$page);
    }

    /**
     * 接单
     * @return array|bool
     */
    public function actionTaking()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        $model = new OrderReceiv();
        return $model->taking($order_sn,self::$userid);
    }

    /**
     * 我的接单
     * @return array
     */
    public function actionTakingList()
    {
        $get = Yii::$app->request->get();
        $page = $get['page']??1;
        $status = $get['status']??'';
        $model = new OrderReceiv();
        return $model->takingList(self::$userid,$status,$page);
    }

    /**
     * 详情
     * @return array
     */
    public function actionDetails()
    {
        $get = Yii::$app->request->get();
        $order_sn = $get['order_sn']??'';
        if (!$order_sn){
            return commonApi::message('请选择要查看的订单',false);
        }
        $model = new OrderReceiv();
        return $model->getDetails($order_sn,self::$userid);
    }
    /**
     * 取货
     * @return array|mixed
     */
    public function actionPickup()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        $model = new OrderReceiv();
        return $model->pickup($order_sn,self::$userid);
    }

    /**
     * 送达
     * @return array|mixed
     */
    public function actionDeliver()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        $model = new OrderReceiv();
        return $model->deliver($order_sn,self::$userid);
    }

    /**
     * 取消
     * @return array|mixed
     */
    public function actionCancel()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        $model = new OrderReceiv();
        return $model->cancel($order_sn,self::$userid);
    }
}